package com.e_manager.soft.emanager.controller;

import com.e_manager.soft.emanager.annotation.Authorization;
import com.e_manager.soft.emanager.annotation.BaseController;
import com.e_manager.soft.emanager.annotation.CurrentUser;
import com.e_manager.soft.emanager.entity.Equipment;
import com.e_manager.soft.emanager.entity.EquipmentHome;
import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.entity.UserEquipment;
import com.e_manager.soft.emanager.entity.parameter.UserEquipmentParameter;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.EquipmentHomeService;
import com.e_manager.soft.emanager.service.EquipmentService;
import com.e_manager.soft.emanager.service.UserEquipmentService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author : lip[lip4051@163.com]
 * @create : 2018/3/21 0021 上午 11:00
 */
@BaseController
@Authorization
@Api(value = "/equipment", description = "用户设备请求controller")
public class UserEquipmentController {
    @Autowired
    private UserEquipmentService userEquipmentService;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private EquipmentHomeService equipmentHomeService;

    @ApiOperation(value = "查询用户设备信息")
    @PostMapping(value = "/findUserEquipments", produces = {"application/json;charset=utf-8"})
    public ResultInfo<PageInfo> findUserEquipments(@RequestBody UserEquipmentParameter userEquipmentParameter, @CurrentUser User iuser) {
        ResultInfo<PageInfo> info = new ResultInfo<>();
        PageInfo<UserEquipment> pageInfo = null;
        try {
            userEquipmentParameter.setUserId(iuser.getUserID());
            pageInfo = userEquipmentService.findUserEquipmentByPage(userEquipmentParameter);
            info.setData(pageInfo);
            info.setMsg(ResultConstant.SUCCESS.getMessage());
            info.setCode(ResultConstant.SUCCESS.getCode());
        } catch (Exception e) {
            info.setMsg(ResultConstant.GET_MESSAGE_FIIL.getMessage());
            info.setCode(ResultConstant.GET_MESSAGE_FIIL.getCode());
            e.printStackTrace();
        }
        return info;
    }

    @ApiOperation(value = "添加用户设备信息")
    @PostMapping(value = "/insertUserEquipment", produces = {"application/json;charset=utf-8"})
    public ResultInfo<?> insertUserEquipment(@RequestBody Equipment equipment, @CurrentUser User iuser) {
        ResultInfo<?> info = new ResultInfo<>();
//        根据用户设备编号查看设备表是否存在此设备 如果存在 就更新该设备信息
        try {
            if (StringUtil.isNotEmpty(equipment.getEquipmentCode())) {
                Equipment e = equipmentService.getEquipmentById(equipment.getEquipmentCode());
                if (e != null) {
//                更新设备
                    equipmentService.updateEquipment(equipment);
                    info.setMsg(ResultConstant.SUCCESS.getMessage());
                    info.setCode(ResultConstant.SUCCESS.getCode());
                } else {
//                插入设备
                    equipmentService.insertEquipment(equipment);

                    info.setMsg(ResultConstant.SUCCESS.getMessage());
                    info.setCode(ResultConstant.SUCCESS.getCode());
                }
//                构建一个用户设备关联表对象 更新关联表数据
                UserEquipment userEquipment = new UserEquipment();
                userEquipment.setUserID(iuser.getUserID());
                userEquipment.setCreateUser(iuser.getUserID());
                userEquipment.setEquipmentCode(equipment.getEquipmentCode());
                userEquipment.setRemarks(equipment.getRemarks());
                userEquipment.setOwn(equipment.getOwn());
                int count = userEquipmentService.getUserEquipmentByNameAndId(userEquipment);
                if (count == 0) {
                    //用户设备关联表插入数据
                    userEquipmentService.insertUserEquipment(userEquipment);
                }
            } else {
                info.setMsg(ResultConstant.EQUIPMENT_NOT_EXESITS.getMessage());
                info.setCode(ResultConstant.EQUIPMENT_NOT_EXESITS.getCode());
            }

        } catch (Exception e) {
            e.printStackTrace();
            info.setMsg(ResultConstant.INSERT_FAIL.getMessage());
            info.setCode(ResultConstant.INSERT_FAIL.getCode());
        }
        return info;
    }

    @ApiOperation(value = "删除用户设备信息")
    @PostMapping(value = "/deleteEquipment", produces = {"application/json;charset=utf-8"})
    public ResultInfo<?> deleteEquipment(@RequestBody Equipment equipment, @CurrentUser User iuser) {
        ResultInfo<?> info = new ResultInfo<>();
        equipment.setCreateUser(iuser.getUserID());
        try {
            switch (equipment.getOwn()) {
                case "0":
                    equipmentService.deleteEquipment(equipment);
                    equipmentService.deleteEquipmentName(equipment);
                    break;
                case "1":
                    equipmentService.deleteEquipmentName(equipment);
                    break;
            }
            info.setMsg(ResultConstant.SUCCESS.getMessage());
            info.setCode(ResultConstant.SUCCESS.getCode());
        } catch (Exception e) {
            info.setMsg(ResultConstant.FAIL.getMessage());
            info.setCode(ResultConstant.FAIL.getCode());
            e.printStackTrace();
        }
        return info;
    }

    @ApiOperation(value = "修改设备信息")
    @PostMapping(value = "/updateEquipment", produces = {"application/json;charset=utf-8"})
    public ResultInfo<?> updateEquipment(@RequestBody Equipment equipment, @CurrentUser User iuser) {
        ResultInfo<?> info = new ResultInfo<>();
        equipment.setCreateUser(iuser.getUserID());
        if (equipment != null) {
            switch (equipment.getOwn()) {
                case "0":
                    equipmentService.updateEquipment(equipment);
                    break;
                case "1":
                    equipmentService.updateEquipmentName(equipment);
                    break;
            }
            info.setMsg(ResultConstant.SUCCESS.getMessage());
            info.setCode(ResultConstant.SUCCESS.getCode());
        } else {
            info.setMsg(ResultConstant.EQUIPMENT_NOT_EXESITS.getMessage());
            info.setCode(ResultConstant.EQUIPMENT_NOT_EXESITS.getCode());
        }
        return info;
    }

    @ApiOperation(value = "android首页设备信息查询")
    @PostMapping(value = "/findEquipmentHomeData", produces = {"application/json;charset=utf-8"})
    public ResultInfo<List<EquipmentHome>> findEquipmentData(@CurrentUser User iuser) {
        ResultInfo<List<EquipmentHome>> info = new ResultInfo<>();
        List<EquipmentHome> list = equipmentHomeService.findEquipmentDatas(iuser.getUserID());
        info.setMsg(ResultConstant.SUCCESS.getMessage());
        info.setCode(ResultConstant.SUCCESS.getCode());
        info.setData(list);
        return info;
    }
}
